<template>
  <el-select v-model="elModel" :clearable="clearable" @clear="clearHandle">
    <el-option :value="valueId" :label="valueTitle" >
      <el-tree
        id="tree-option"
        ref="selectTree"
        :accordion="accordion"
        :data="options"
        show-checkbox
        :props="props"
        :node-key="props.value"
        :default-expanded-keys="defaultExpandedKey"
        @check="handleCheck"
        :check-strictly="true">
      </el-tree>
    </el-option>
  </el-select>
</template>

<script>
  export default {
    name: "HeyTree",
    model: {
      prop: 'model',
      event: 'set-model'
    },
    props:{
      // 配置项
      props:{
        type: Object,
        default: {
          value:'id',             // ID字段名
          label: 'label',         // 显示名称
          children: 'children'    // 子级字段名
        }
      },
      model: {
        required: true,
        type: [String, Array, Object]
      },
      // 选项列表数据(树形结构的对象数组)
      options:{ type: Array, default: [] },

      // 默认展开项
      expanded:{ type: String, default: '' },

      // 可清空选项
      clearable:{ type:Boolean, default: true },

      // 自动收起
      accordion:{ type:Boolean, default: false }
    },
    data() {
      return {
        elModel: '',
        valueId: null,
        valueTitle:'',
        defaultExpandedKey:[]
      }
    },
    mounted(){
      this.elModel = this.model
      this.valueId = this.model    // 初始值
      this.initHandle()
    },
    methods: {
      handleCheck (a, b) {
        //a为传递给 data 属性的数组中该节点所对应的对象；b为树目前的选中状态对象
        if (b.checkedKeys.length > 0) {
          this.$refs.selectTree.setCheckedKeys([a[this.props.value]])
          this.valueId = a[this.props.value]
          this.valueTitle = a[this.props.label]
          this.$emit('getValue',this.valueId)
          this.$emit('set-model', this.valueId)
        }else{
          this.clearHandle()
        }
      },
      // 初始化值
      initHandle(){
        if(this.valueId){
          this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label]
          this.$refs.selectTree.setCheckedKeys([this.valueId])
          this.defaultExpandedKey = [this.valueId]      // 设置默认展开
        }else{
          this.defaultExpandedKey = [this.expanded]
        }
        this.initScroll()
      },
      // 初始化滚动条
      initScroll(){
        this.$nextTick(()=>{
          let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
          let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
          scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
          scrollBar.forEach(ele => ele.style.width = 0)
        })
      },
      // 清除选中
      clearHandle(){
        this.valueTitle = ''
        this.valueId = null
        this.defaultExpandedKey = []
        this.clearSelected()
        this.$refs.selectTree.setCheckedKeys([])
        this.$emit('getValue',null)
        this.$emit('set-model', '')
      },
      // 清空选中样式
      clearSelected(){
        let allNode = document.querySelectorAll('#tree-option .el-tree-node')
        allNode.forEach((element)=>element.classList.remove('is-current'))
      }
    },
    watch: {
      model(newVal) {
        this.elModel = newVal
      }
    },
  }
</script>

<style scoped>
  .el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
    height: auto;
    max-height: 274px;
    padding: 0;
    overflow: hidden;
    overflow-y: auto;
  }
  .el-select-dropdown__item.selected{
    font-weight: normal;
  }
  ul li >>>.el-tree .el-tree-node__content{
       height:auto;
       padding: 0 20px;
     }
  .el-tree-node__label{
    font-weight: normal;
  }
  .el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
    font-weight: normal;
  }
</style>
